1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| public class MyTest{ public static void main(String[] args) { List<Map<String, String>> postmans = new ArrayList<>();
postmans.add(createMap("2016-01", "1", "420000", "false")); postmans.add(createMap("2016-01", "2", "440000", "false")); postmans.add(createMap("2016-02", "3", "420000", "false")); postmans.add(createMap("2016-03", "4", "420000", "true")); postmans.add(createMap("2016-03", "5", "410000", "false")); postmans.add(createMap("2016-03", "6", "440000", "true")); postmans.add(createMap("2016-04", "7", "420000", "false")); postmans.add(createMap("2016-04", "8", "440000", "false"));
System.out.println(JSONObject.toJSONString(postmans));
Map<String, Map<Boolean, Map>> data = postmans.stream() .collect(Collectors.groupingBy(d -> d.get("date"), TreeMap::new, Collectors.partitioningBy(d-> d.get("type").equals("true"), Collectors.reducing(newHashMap(), (left, right)->{
System.out.println("开始:"+JSONObject.toJSONString(left)+","+JSONObject.toJSONString(right));
Object code = right.get("code"); Object number = right.get("number"); left.put(code, number); System.out.println("结果:"+JSONObject.toJSONString(left)+"\n");
return left; })))); System.out.println(JSONObject.toJSONString(data));
Map<String, Map<Boolean, Map<String, String>>> test = postmans.stream() .collect(Collectors.groupingBy(d -> d.get("date"), TreeMap::new, Collectors.partitioningBy(d -> d.get("type").equals("true"), Collectors.reducing(newHashMap(), (left, right) -> { Map<String, String> map = new HashMap<>();
System.out.println("开始:"+JSONObject.toJSONString(left)+","+JSONObject.toJSONString(right));
String leftCode = left.get("code"); String leftNumber = left.get("number");
if (leftCode == null) { map.putAll(left); } else { map.put(leftCode, leftNumber); }
String rightCode = right.get("code"); String rightNumber = right.get("number");
map.put(rightCode, rightNumber);
System.out.println("结果:"+JSONObject.toJSONString(map)+"\n");
return map; })))); System.out.println(JSONObject.toJSONString(test));
}
private static Map<String, String> newHashMap(){ Map<String, String> map = new HashMap<>(); map.put("time"+System.currentTimeMillis(), "测试:"+System.currentTimeMillis()); return map; }
private static Map<String,String> createMap(String date, String number, String code, String type){ Map<String, String> map = new HashMap<>(); map.put("date", date); map.put("number", number); map.put("code", code); map.put("type", type); return map; } }
|